package com.example.chat_matching.services;

import com.example.chat_matching.common.Result;
import com.example.chat_matching.common.ResultCode;
import com.example.chat_matching.demo.*;
import com.example.chat_matching.mapper.RoomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class MatchService {
    @Autowired
    private RoomMapper roomMapper;


    /**
     * date: 2025/01/12 15:09<br/>
     * Description: 匹配
     * @author: jmy <br/>
     */
    public Result match(MatchPeople matchPeople){
        //0man 1women
        for (Room room : RoomList.roomList){
            for(MatchPeople people : room.matchPeoples){
                if (people.getId() == matchPeople.getId()){
                    return Result.failed(ResultCode.ROOM_EXIT);
                }
            }
        }
        //男匹配女
        if (Objects.equals(Sex.MEN.getSex(),matchPeople.getUserSex()) || Objects.equals(Sex.WOMEN.getSex(),matchPeople.getIntentionSex())){
            if(!MatchList.girlIntentionList.isEmpty()){
                MatchPeople beSelectPeople = MatchList.girlIntentionList.get(0);
                MatchList.girlIntentionList.remove(0);
                Room room = new Room();
                room.matchPeoples.add(matchPeople);
                room.matchPeoples.add(beSelectPeople);
                roomMapper.createRoom(room);
                return Result.success(room);
            }else {
                MatchList.boyIntentionList.add(matchPeople);
                return Result.success(ResultCode.ATTENTION_SEX_NULL);
            }
        }
        //男匹配男
        if (Objects.equals(Sex.MEN.getSex(),matchPeople.getUserSex()) || Objects.equals(Sex.MEN.getSex(),matchPeople.getIntentionSex())){
            if (!MatchList.boyIntentionList.isEmpty()){
                MatchPeople beSelectPeople = MatchList.boyIntentionList.get(0);
                MatchList.boyIntentionList.remove(0);
                Room room = new Room();
                room.matchPeoples.add(matchPeople);
                room.matchPeoples.add(beSelectPeople);
                roomMapper.createRoom(room);
                return Result.success(room);
            }else {
                MatchList.boyIntentionList.add(matchPeople);
                return Result.success(ResultCode.ATTENTION_SEX_NULL);
            }
        }
        //女匹配男
        if (Objects.equals(Sex.WOMEN.getSex(),matchPeople.getUserSex()) || Objects.equals(Sex.MEN.getSex(),matchPeople.getIntentionSex())){
            if (!MatchList.boyIntentionList.isEmpty()){
                MatchPeople beSelectPeople = MatchList.boyIntentionList.get(0);
                MatchList.boyIntentionList.remove(0);
                Room room = new Room();
                room.matchPeoples.add(matchPeople);
                room.matchPeoples.add(beSelectPeople);
                roomMapper.createRoom(room);
                return Result.success(room);
            }else {
                MatchList.girlIntentionList.add(matchPeople);
                return Result.success(ResultCode.ATTENTION_SEX_NULL);
            }
        }
        //女匹配女
        if (Objects.equals(Sex.WOMEN.getSex(),matchPeople.getUserSex()) || Objects.equals(Sex.WOMEN.getSex(),matchPeople.getIntentionSex())){
            if (!MatchList.girlIntentionList.isEmpty()){
                MatchPeople beSelectPeople = MatchList.girlIntentionList.get(0);
                MatchList.girlIntentionList.remove(0);
                Room room = new Room();
                room.matchPeoples.add(matchPeople);
                room.matchPeoples.add(beSelectPeople);
                roomMapper.createRoom(room);
                return Result.success(room);
            }else {
                MatchList.girlIntentionList.add(matchPeople);
                return Result.success(ResultCode.ATTENTION_SEX_NULL);
            }
        }
        return Result.failed();
    }
    /**
     * date: 2025/01/12 17:06<br/>
     * Description:结束会话
     * @param roomId 房间号
     * @author: jmy <br/>
     */
    public Result endConversation(Integer roomId){
        //遍历roomList 删除相应房间
        for (int i = 0;i < RoomList.roomList.size();i++){
            if (roomId == i){
                RoomList.roomList.remove(i);
                return Result.success(RoomList.roomList);
            }
        }
        return Result.failed(ResultCode.ROOM_NOT_EXIT);
    }

    /**
     * date: 2025/01/12 19:06<br/>
     * Description: 取消匹配
     * @param userSex 用户性别
     * @param userId 用户id
     * @author: jmy <br/>
     */
    public Result cancleMatch(Integer userSex,Integer userId){
        if (userSex == Sex.MEN.getSex()){
            int i = 0;
            for (MatchPeople people : MatchList.boyIntentionList){
                if (userId == people.getId()){
                    MatchList.boyIntentionList.remove(i);
                    return Result.success(MatchList.boyIntentionList);
                }else {
                    i++;
                }
            }
        }else {
            int i = 0;
            for (MatchPeople people : MatchList.girlIntentionList){
                if (userId == people.getId()){
                    MatchList.girlIntentionList.remove(i);
                    return Result.success(MatchList.girlIntentionList);
                }else {
                    i++;
                }
            }
        }
        return new Result();
    }
}
